﻿using B.S.BaseData.Infrastructure.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace B.S.BaseData.Infrastructure.Implement
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly BaseDbContext db;

        public BaseRepository(BaseDbContext db)
        {
            this.db = db;
        }
        public async Task<int> CreateAsync(T entity)
        {
            await db.Set<T>().AddAsync(entity);
            return await db.SaveChangesAsync();
        }

        public async Task<int> Delete(T t)
        {
            db.Set<T>().Remove(t);
            return await db.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            return db.Set<T>();
        }

        public async Task<T> GetValue(long id)
        {
            return await db.Set<T>().FindAsync(id);
        }

        public async Task<T> GetValues(Expression<Func<T, bool>> expression)
        {
            return await db.Set<T>().Where(expression).FirstOrDefaultAsync();
        }

        public async Task<int> UpdateAsync(T entity)
        {
            db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
    }
}
